/**
 * @author AdinZ
 * @date 2020/11/9 15:44
 */
import { Notification, Message, MessageBox, Loading } from 'element-ui'
import axios from '@/utils/request'
import router from '@/router'
import store from "@/store";
import {parseTime} from '@/utils/index'
function message(msg, title) {
  if (!title) title = '提示'
  Notification({ title: title, message: msg, type: 'success' })
}
function error(msg) {
  Message({ message: msg, type: 'error' })
}
function confirm(msg) {
  return MessageBox.confirm(msg, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
}


function request(option) {
  return new Promise((resolve, reject) => {
    let loadding = Loading.service({
      fullscreen: true,
      text: '网络请求中....'
    })
    axios(option)
      .then((res) => {
        if (loadding) {
          loadding.close()
          loadding = null
        }
        if (res.code == 200) {
          resolve(res)
        } else if(res.code===401){
          r('/401')
          reject(res)
        } else if(res.code===403){
          r('/login')
          reject(res)
        }else {
          error(res.message)
          reject(res)
        }
      })
      .catch((e) => {
        if (loadding) {
          loadding.close()
          loadding = null
        }
        reject(e)
        error(e.message)
      })
  })
}

function go(url, params) {
  if (url && url.length > 0) {
    if (url.startsWith('http')) window.open(url)
    else router.push({ path: url, query: params })
  }
}
function back(i) {
  if (!i) i = -1
  router.back(i)
}
function to(url, params) {
  go(url, params)
}
function r(url, params) {
  if (url && url.length > 0) {
    if (url.startsWith('http://') || url.startsWith('https://')) location.replace(url)
    else router.replace({ path: url, query: params })
  }
}

function post(url, parame, data) {
  return request({
    url: url,
    method: 'post',
    params: parame,
    data
  })
}

function postConfirm(msg, url, parame, data) {
  return new Promise((resolve, reject) => {
    confirm(msg)
      .then(() => {
        post(url, parame, data)
          .then((res) => {
            resolve(res)
          })
          .catch((e) => {
            reject(e)
          })
      })
      .catch((e) => {})
  })
}
function get(url, params) {
  return request({
    url: url,
    method: 'get',
    params: params
  })
}
function put(url, params, data) {
  return request({
    url: url,
    method: 'put',
    params: params,
    data
  })
}
function deleteConfirm(url, parame) {
  var _this = this
  return new Promise((resolve, reject) => {
    confirm('确定要删除吗？')
      .then(() => {
        _this
          .deletes(url, parame)
          .then((res) => {
            resolve(res)
          })
          .catch((e) => {
            reject(e)
          })
      })
      .catch((e) => {
        reject(e)
      })
  })
}
function deletes(url, params) {
  return request({
    url: url,
    method: 'delete',
    params: params
  })
}


/**
 * 菜单id
 * @param mid
 * @returns {boolean}
 */
function isMenu(mid) {
  let list = store.state.user.menus;
  if(!list)return false;
  return list.includes(mid);
}


export default {
  message,
  error,
  confirm,
  post,
  get,
  put,
  deletes,
  deleteConfirm,
  postConfirm,
  go,
  to,
  back,
  r,
  parseTime,
  isMenu
}
